package com.wc.AlgoOJ_homework.AlgoOJ_BL4.H_岛屿个数;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/6 10:18
 * @description http://43.138.190.70:8888/p/1124?tid=660a4f84d7ba43eb2f4c549d
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 55;
    static char[][] g = new char[N][N];
    static int[] dx = new int[]{-1, 0, 1, 0, -1, -1, 1, 1};
    static int[] dy = new int[]{0, -1, 0, 1, -1, 1, -1, 1};
    static boolean[][] st = new boolean[N][N];
    static int t, n, m;
    static int res = 0;

    public static void main(String[] args) {
        t = sc.nextInt();
        Arrays.fill(g[0], '0');
        while (t-- > 0) {
            res = 0;
            for (int i = 0; i <= n + 1; i++) Arrays.fill(st[i], false);
            n = sc.nextInt();
            m = sc.nextInt();
            for (int i = 1; i <= n; i++) {
                g[i] = ("0" + sc.next() + "0").toCharArray();
            }
            g[n + 1] = new char[N];
            Arrays.fill(g[n + 1], '0');
            dfs(0, 0);
            out.println(res);
        }
        out.flush();
    }

    static void dfs(int x, int y) {
        st[x][y] = true;
        for (int i = 0; i < 8; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (!isBorder(nx, ny) && !st[nx][ny]) {
                if (g[nx][ny] == '0') {
                    dfs(nx, ny);
                } else {
                    res++;
                    paint(nx, ny);
                }
            }
        }
    }

    static void paint(int x, int y) {
        st[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (!isBorder(nx, ny) && !st[nx][ny] && g[nx][ny] == '1') {
                paint(nx, ny);
            }
        }
    }

    static boolean isBorder(int x, int y) {
        return x < 0 || x > n + 1 || y < 0 || y > m + 1;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
